package com.leilei.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leilei.eduservice.entity.EduChapter;
import com.leilei.eduservice.entity.EduVideo;
import com.leilei.eduservice.entity.chapter.ChapterVo;
import com.leilei.eduservice.entity.chapter.VideoVo;
import com.leilei.eduservice.mapper.EduChapterMapper;
import com.leilei.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leilei.eduservice.service.EduVideoService;
import com.leilei.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-05-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    //注入小节的service
    @Autowired
    private EduVideoService eduVideoService;


    //课程大纲列表,根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1、查询章节的数据
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(queryWrapper);

        //2、查询小节
        QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(queryWrapper1);

        //创建list集合，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();


        //3、遍历查询章节list集合进行封装
        for (int i = 0; i < eduChapterList.size(); i++) {
            //每个章节
            EduChapter eduChapter = eduChapterList.get(i);

            ChapterVo chapterVo = new ChapterVo();

            BeanUtils.copyProperties(eduChapter,chapterVo);

            //把chapterVo放在finalList中
            finalList.add(chapterVo);

            //创建集合，用于封装章节的小节
            List<VideoVo> videoVoList = new ArrayList<>();

            //4、遍历查询小节list集合，进行封装
            for (int m = 0; m < eduVideos.size(); m++) {
                EduVideo eduVideo = eduVideos.get(m);

                //判断小节的chapterId和章节里面的id是否一样
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);

                    //放到小节封装集合
                    videoVoList.add(videoVo);
                }


            }
            //把封装之后的小节list集合，放到章节对象里面
            chapterVo.setChildren(videoVoList);
        }


        return finalList;
    }


    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查询小节表，如果查询到数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        //判断
        if (count >0){    //查询到小节，不进行删除
            throw  new GuliException(20001,"不能删除");
        }else {     //不能查询到数据，进行删除
            //删除章节
            int result = baseMapper.deleteById(chapterId);
            return  result>0;
        }
    }


    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}
